package ThaumPerditio.common.tile;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB;
import thaumcraft.api.TileThaumcraft;

public class TileDefusionPedestal extends TileThaumcraft implements ISidedInventory{
    private static final int[] slots = { 0 };
    private ItemStack[] inventory = new ItemStack[1];
    private String customName;

    @SideOnly(Side.CLIENT)
    public AxisAlignedBB getRenderBoundingBox()
    {
        return AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 2, this.zCoord + 1);
    }

    public int getSizeInventory()
    {
        return 1;
    }

    public ItemStack getStackInSlot(int slot)
    {
        return this.inventory[slot];
    }

    public ItemStack decrStackSize(int slot, int count)
    {
        if (this.inventory[slot] != null)
        {
            if (!this.worldObj.isRemote) {
                this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
            }
            if (this.inventory[slot].stackSize <= count)
            {
                ItemStack itemstack = this.inventory[slot];
                this.inventory[slot] = null;
                markDirty();
                return itemstack;
            }
            ItemStack itemstack = this.inventory[slot].splitStack(count);
            if (this.inventory[slot].stackSize == 0) {
                this.inventory[slot] = null;
            }
            markDirty();
            return itemstack;
        }
        return null;
    }

    public ItemStack getStackInSlotOnClosing(int slot)
    {
        if (this.inventory[slot] != null)
        {
            ItemStack itemstack = this.inventory[slot];
            this.inventory[slot] = null;
            return itemstack;
        }
        return null;
    }

    public void setInventorySlotContents(int slot, ItemStack istk)
    {
        this.inventory[slot] = istk;
        if ((istk != null) && (istk.stackSize > getInventoryStackLimit())) {
            istk.stackSize = getInventoryStackLimit();
        }
        markDirty();
        if (!this.worldObj.isRemote) {
            this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
        }
    }

    public String getInventoryName()
    {
        return hasCustomInventoryName() ? this.customName : "container.pedestal";
    }

    public boolean hasCustomInventoryName()
    {
        return (this.customName != null) && (this.customName.length() > 0);
    }

    public void setGuiDisplayName(String name)
    {
        this.customName = name;
    }

    public void readCustomNBT(NBTTagCompound tagc)
    {
        NBTTagList tagl = tagc.getTagList("Items", 10);
        this.inventory = new ItemStack[getSizeInventory()];
        for (int i = 0; i < tagl.tagCount(); i++)
        {
            NBTTagCompound tagc1 = tagl.getCompoundTagAt(i);
            byte b0 = tagc1.getByte("Slot");
            if ((b0 >= 0) && (b0 < this.inventory.length)) {
                this.inventory[b0] = ItemStack.loadItemStackFromNBT(tagc1);
            }
        }
    }

    public void writeCustomNBT(NBTTagCompound tagc)
    {
        NBTTagList tagl = new NBTTagList();
        for (int i = 0; i < this.inventory.length; i++) {
            if (this.inventory[i] != null)
            {
                NBTTagCompound tagl1 = new NBTTagCompound();
                tagl1.setByte("Slot", (byte)i);
                this.inventory[i].writeToNBT(tagl1);
                tagl.appendTag(tagl1);
            }
        }
        tagc.setTag("Items", tagl);
    }

    public void readFromNBT(NBTTagCompound nbtc)
    {
        super.readFromNBT(nbtc);
        if (nbtc.hasKey("CustomName")) {
            this.customName = nbtc.getString("CustomName");
        }
    }

    public void writeToNBT(NBTTagCompound nbtc)
    {
        super.writeToNBT(nbtc);
        if (hasCustomInventoryName()) {
            nbtc.setString("CustomName", this.customName);
        }
    }

    public int getInventoryStackLimit()
    {
        return 1;
    }


    public boolean canUpdate()
    {
        return false;
    }

    public boolean isUseableByPlayer(EntityPlayer player)
    {
        return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) == this;
    }

    public void openInventory() {}

    public void closeInventory() {}

    public boolean isItemValidForSlot(int slot, ItemStack istk)
    {
        return true;
    }

    public int[] getAccessibleSlotsFromSide(int side)
    {
        return slots;
    }

    public boolean canInsertItem(int slot, ItemStack istk, int side)
    {
        return getStackInSlot(slot) == null;
    }

    public boolean canExtractItem(int slot, ItemStack istk, int side)
    {
        return true;
    }

}
